#include "RWShapeSerializeManager.h"
#include <BRepTools.hxx>
#include <BRep_Builder.hxx>

TopoDS_Shape RWShapeSerializeManager::LoadShape(const std::string& filename)
{
	TopoDS_Shape loadedShape;
	BRep_Builder builder;

	// 从BREP文件加载
	if (!BRepTools::Read(loadedShape, (std::string("./save/model/") + filename).c_str(), builder)) {
		std::cout << "RWShapeSerializeManager: load model failed" << std::endl;
	}

	return loadedShape;
}

void RWShapeSerializeManager::SaveShape(const std::string& filename, const TopoDS_Shape& shape)
{
	if (filename.empty() || shape.IsNull())
	{
		return;
	}

	// 保存为BREP文件
	if (!BRepTools::Write(shape, (std::string("./save/model/") + filename).c_str())) {
		std::cout << "RWShapeSerializeManager: save model failed" << std::endl;
	}
}

std::string RWShapeSerializeManager::ConstructFilename()
{
	// 初始化随机数种子，注意：只需调用一次
	static bool initialized = false;
	if (!initialized) {
		std::srand(static_cast<unsigned int>(std::time(nullptr)));
		initialized = true;
	}

	return "M_" + std::to_string(std::time(nullptr)) + std::to_string(std::rand() % 11);
}
